package net.scala.chapter3.test

import com.typesafe.scalalogging.slf4j.LazyLogging
import org.junit.{FixMethodOrder, Test}
import org.junit.runners.MethodSorters
import org.scalatest.junit.AssertionsForJUnit

/**
 * Scala 多重继承的菱形问题
 * @author Barudisshu
 */
@FixMethodOrder(MethodSorters.JVM)
class TestMixin extends AssertionsForJUnit with LazyLogging {

  @Test def test() {
    val mixin = Mixin("jijiang")
    mixin.foo("jijiang: ")
  }
}

trait jijiang {
  def foo(msg: String) = println(msg)
}

trait mama extends jijiang {
  val str1 = "mama: "
  override def foo(msg: String) = super.foo(str1.concat(msg))
}

trait papa extends jijiang {
  val str2 = "papa: "
  override def foo(msg: String) = println(str2 + msg)
}

class Mixin private(msg: String) extends jijiang {
  def this() = this("mixin")
}

object Mixin {
  // 如果包含菱形问题，则只执行最右边的
  def apply(msg: String) = new Mixin(msg) with papa with mama

  // 由于Scala的惰性求值问题，包含多重继承的父类中的成员变量名称应该不一样，否则造成编译错误
  // 在Scala中，super关键字是动态调用了，因此super在不同情况表示不同的内容

}
